/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javaseapp - T2
 * <p>Powered by webrx On 2021-08-26 17:28:34
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T2 {
    public final Object lock = new Object();

    void work() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s启动，wait()....%n", tn);
        try {
            synchronized (lock) {
                lock.wait();
            }
            System.out.printf("%s被唤醒...%n", tn);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        var t = new T2();
        for (int i = 0; i < 5; i++) {
            new Thread(t::work, "T" + (i + 1)).start();
        }

        try {
            TimeUnit.SECONDS.sleep(5);
            synchronized (t.lock) {
                //t.lock.notify();
                t.lock.notifyAll();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
